/**
 * System:      oshys
 * Model:       log
 * Licence:     GNU General Public License v3
 * Author:      sunwl
 * Date:        2010/02/19
 */
package org.oshys.util.logging;

import java.io.IOException;
import java.io.Writer;
/**
 * 一个使用流输出Log消息的Appender应该继承该类。<br>
 * 并应该正确的使用本类提供的方法使用和关闭流。
 * @author sunwl
 */
public abstract class StreamAppender extends Appender {

    // protected OutputStream out = null;

    protected Writer writer = null;

    public abstract void append(LogMessage logMessage);

    protected synchronized void flush(String msg) {
        try {
            writer.write(msg);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected synchronized void flushAndClose() {
        try {
            // writer.write(msg);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer = null;
        // out = null;
    }

    protected synchronized void close() {
        try {
            if (this.writer != null) {
                this.writer.close();
                this.writer = null;
            }
            /*
             * if(this.out != null){ this.out.close(); this.out = null; }
             */
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
